'use strict';

const Controller = require('egg').Controller;

const jwt = require('jsonwebtoken');

class UserController extends Controller {
    async login() {
        let {ctx,service} = this;
        let {username,password} = ctx.request.body;

        if(username && password){
            try{
                let target = await service.user.login(username,password);
                if(target.length){
                    let token = jwt.sign({username},'lixd',{expiresIn:60*60*5});
                    ctx.body = {
                        code:1,
                        msg:'登录成功',
                        token
                    }
                }else{
                    ctx.body = {
                        code:0,
                        msg:'登录失败'
                    }
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
}

module.exports = UserController;
